{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# SearxNG Search Tool Walkthrough\n",
    "This notebook demonstrates how to use the SearxNG Search Tool in AG2 to perform real-time web searches using a privacy-friendly, open-source metasearch engine."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "## Installation\n",
    "No API key is required for public SearxNG instances. For private instances, consult your administrator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the SearxngSearchTool\n",
    "from autogen.tools.experimental import SearxngSearchTool"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Basic Usage\n",
    "You can use the tool directly to perform a search and view results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_tool = SearxngSearchTool()\n",
    "results = search_tool(query=\"open source search engine\", max_results=3)\n",
    "for r in results:\n",
    "    print(r[\"title\"], r[\"link\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## Using with AG2 Agents\n",
    "You can register the tool with an AssistantAgent for LLM-driven search."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from autogen import AssistantAgent\n",
    "\n",
    "search_tool = SearxngSearchTool()\n",
    "assistant = AssistantAgent(name=\"assistant\")\n",
    "search_tool.register_for_llm(assistant)\n",
    "response = assistant.run(\n",
    "    message=\"What is SearxNG?\",\n",
    "    tools=assistant.tools,\n",
    "    max_turns=2,\n",
    "    user_input=False,\n",
    ")\n",
    "response.process()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "## Customizing Parameters\n",
    "You can specify categories, language, or a custom SearxNG instance URL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = search_tool(query=\"AI news\", max_results=2, categories=[\"news\"], language=\"en-US\")\n",
    "for r in results:\n",
    "    print(r[\"title\"], r[\"link\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "## Output Format\n",
    "Each result is a dictionary with `title`, `link`, and `snippet` keys."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(results[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "## Learn More\n",
    "- [SearxNG Project](https://searxng.org/)\n",
    "- [AG2 Reference Tools Documentation](/docs/user-guide/reference-tools/searxng-search)"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "This notebook demonstrates how to use the SearxNG Search Tool for real-time web searches using a privacy-friendly, open-source metasearch engine.",
   "tags": [
    "tool",
    "searxng",
    "search",
    "web"
   ]
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
